﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;
using CNotebook;

namespace NotebookS2
{
    public partial class CRYSTAL_STOCKMOVEMENT : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public CRYSTAL_STOCKMOVEMENT()
        {
            InitializeComponent();
        }

        // Connection of Data
        public string ConnectionString { get; set; }
        SqlConnection oConnection = new SqlConnection();
        SqlCommand oCommand = new SqlCommand();
        SqlDataAdapter oAdapter = new SqlDataAdapter();
        DataSet ds = new DataSet();

        // Crystal Report File
        ReportDocument rptDoc = new ReportDocument();
        
        // Parameters
        public string CountDocumentNo { get; set; }
        public string WarehouseCode { get; set; }
        public string ItemCode { get; set; }
        public DateTime AsOfDate { get; set; }
        public string LotNumber { get; set; }

        // Configurable Location of Crystal Report File
        public string GetLayout()
        {
            ReportConfigManager oManager = new ReportConfigManager();
            
            const int _DOCUMENT_TYPE = 45;
            oManager.ConnectionString = this.ConnectionString;

            oManager.Open();

            string _FILE_NAME = oManager.GetReportFileName(_DOCUMENT_TYPE);

            oManager.Close();

            return _FILE_NAME;
        }

        // Process to Get Report using Crystal

        public void ViewStockMovement()
        {
            rptDoc = new ReportDocument();
            
            oConnection = new SqlConnection();
            oCommand = new SqlCommand();
            oAdapter = new SqlDataAdapter();
            ds = new DataSet();

            try
            {
                string sReportFile = "";

                sReportFile = GetLayout();

                rptDoc.Load(sReportFile);


                oConnection.ConnectionString = this.ConnectionString;
                oConnection.Open();

                oCommand.Connection = oConnection;
                oCommand.CommandType = CommandType.StoredProcedure;
                oCommand.CommandText = "SP_STOCKMOVEMENT";

                //@PC_DOCUMENT NVARCHAR(50), @WHSECODE NVARCHAR(50), @ITEMCODE NVARCHAR(50), @ASOFDATE DATETIME
                oCommand.Parameters.Add(new SqlParameter("@PC_DOCUMENT", this.CountDocumentNo));
                oCommand.Parameters.Add(new SqlParameter("@WHSECODE", this.WarehouseCode));
                oCommand.Parameters.Add(new SqlParameter("@ITEMCODE", this.ItemCode));
                oCommand.Parameters.Add(new SqlParameter("@ASOFDATE", this.AsOfDate));
                oCommand.Parameters.Add(new SqlParameter("@LOTNUMBER", this.LotNumber));

                oAdapter.SelectCommand = oCommand;
                oAdapter.Fill(ds);

                oConnection.Close();

                rptDoc.SetDataSource(ds.Tables[0]);


                CRViewer.ReportSource = rptDoc;
                CRViewer.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
          
        }

        private void CRYSTAL_SALESORDER_Load(object sender, EventArgs e)
        {
            ViewStockMovement();
        }

        private void CRYSTAL_STOCKMOVEMENT_FormClosing(object sender, FormClosingEventArgs e)
        {
            rptDoc.Dispose();
            oConnection.Dispose();
            oCommand.Dispose();
            oAdapter.Dispose();
            ds.Dispose();
        }

        private void CRViewer_Load(object sender, EventArgs e)
        {

        }
    }
}
